package com.banciyuan.bcywebview.biz.detail.mixweb.bridge;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;

import com.banciyuan.bcywebview.biz.detail.mixweb.MixWebImageViewActivity;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.core.assist.FailReason;
import com.banciyuan.bcywebview.utils.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.Complex;

/**
 * Created by squall on 2016/11/23.
 */

public class ImageBridge {

    private Context mContext;
    private Complex complex;
    private OnLoadImageSucc onLoadImageSucc;

    public ImageBridge(Context c) {
        this.mContext = c;
    }

    public void setComplex(Complex complex) {
        this.complex = complex;
    }

    @JavascriptInterface
    public boolean isAutoLoadImage() {
        return true;
    }

    @JavascriptInterface
    public String readImageCache(String url) {
        File file = ImageLoader.getInstance().getDiskCache().get(url);
        if (file != null && file.exists()) {
            return "file://" + file.getAbsolutePath();
        } else {
            return "";
        }
    }

    @JavascriptInterface
    public void loadImage(String url) {
        ImageLoader.getInstance().loadImage(url, null, ImageOptions.optionsCopy, new SimpleImageLoadingListener() {
            @Override
            public void onDiskCacheComplete(String imageUri, File mfile, String type) {
                if (onLoadImageSucc != null) {
                    onLoadImageSucc.onSucc(imageUri, "file://" + mfile.getAbsolutePath());
                }
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                if (onLoadImageSucc != null) {
                    onLoadImageSucc.onFail(imageUri);
                }
            }
        }, null, false);
    }

    @JavascriptInterface
    public void openImage(String url, int index) {
        Gson gson = new Gson();
        List<String> urls = gson.fromJson(url, new TypeToken<List<String>>() {
        }.getType());
        Intent intent = new Intent(mContext, MixWebImageViewActivity.class);
        intent.putStringArrayListExtra("imageUrls", (ArrayList<String>) urls);
        intent.putExtra("position", index);
        intent.putExtra("right_click", complex.getProperties().isForbidden_right_click());
        mContext.startActivity(intent);
    }

    @JavascriptInterface
    public void bridgeLog(String log) {
        Log.i("image===", log);
    }

    public void setOnLoadImageSucc(OnLoadImageSucc onLoadImageSucc) {
        this.onLoadImageSucc = onLoadImageSucc;
    }

    public interface OnLoadImageSucc {
        void onSucc(String imageUri, String localUri);

        void onFail(String imageUri);
    }
}
